// Created: Dec 14, 2014
//
// Author: wjinwen.1988@gmail.com

#ifndef CHI_NET_INET_ADDRESS_H
#define CHI_NET_INET_ADDRESS_H

#include <netinet/in.h>

#include <string>

namespace chi {
namespace net {

typedef struct sockaddr SA;

const SA* sockaddr_cast(const struct sockaddr_in* addr);
SA* sockaddr_cast(struct sockaddr_in* addr);

// sockaddr_in封装
class InetAddress {
 public:
  explicit InetAddress(uint16_t port = 0, bool loopback_only = false);
  InetAddress(std::string&& ip, uint16_t port);
  InetAddress(const struct sockaddr_in& addr);

  std::string ToIp() const;
  std::string ToIpPort() const;

  const struct sockaddr_in& addr() const { return addr_; }
  void set_addr(const struct sockaddr_in& addr) { addr_ = addr; }

  // 根据hostname获取IP地址
  static bool Resolve(std::string&& hostname, InetAddress* result);
 private:
  struct sockaddr_in addr_;
};

} 
}

#endif

